package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.PmsAsProductClassify;
import com.zjxf.base.bean.ro.RoPmsAsProductClassify;
import com.zjxf.base.bean.vo.VoPmsAsProductClassify;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.PmsAsProductClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 农副产品分类
 *
 * @author ZhangWei
 * @date 2021/4/6/006 16:55
 */
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "农副产品分类")
@RequestMapping("/pmsAsProductClassify/")
public class PmsAsProductClassifyController extends BaseController {

    private final PmsAsProductClassifyService pmsAsProductClassifyService;

    @ApiOperation(value = "商家添加或修改")
    @PostMapping(value = "sellAddOrUpdate")
    public JSONResponse selladdOrUpdate(@Valid @RequestBody VoPmsAsProductClassify.SelladdOrUpdate vo) {
        pmsAsProductClassifyService.sellAddOrUpdate(vo);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public JSONResponse delete(@RequestParam("id") Long id) throws OperationException {
        pmsAsProductClassifyService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "回显")
    @GetMapping("get")
    public JSONResponse get(@RequestParam("id") Long id) throws OperationException {
        PmsAsProductClassify pmsAsProductClassify = pmsAsProductClassifyService.getEntity(id);
        RoPmsAsProductClassify.Details details = pmsAsProductClassifyService.getEntityRo(pmsAsProductClassify);
        return JSONResponse.success(details);
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public JSONResponse page(@Valid @RequestBody VoPmsAsProductClassify.filter filter) {
        Page<PmsAsProductClassify> pmsAsProductClassifyPage = pmsAsProductClassifyService.page(filter);
        JSONObject jsonData = pmsAsProductClassifyService.pageRo(pmsAsProductClassifyPage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "集合")
    @GetMapping("adminList")
    public JSONResponse adminList() {
        List<PmsAsProductClassify> pmsAsProductClassifyList = pmsAsProductClassifyService.getList();
        List<RoPmsAsProductClassify.ListWithChild> listWithChildren = pmsAsProductClassifyService.listWithChildren(pmsAsProductClassifyList);
        return JSONResponse.success(listWithChildren);
    }
}
